package org.example.ui;

import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SidebarExample extends Application {

    private static final int SIDEBAR_PREF_WIDTH = 200;

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root, 800, 600, Color.WHITE);

        // 创建顶部布局HBox
        HBox topBox = new HBox();
        topBox.setPadding(new Insets(10));
        topBox.setStyle("-fx-background-color: #DDDDDD;");

        Button toggleSidebarBtn = new Button("打开侧边栏");
        topBox.getChildren().add(toggleSidebarBtn);

        // 创建侧边栏布局VBox
        VBox sidebar = new VBox();
        sidebar.setPrefWidth(SIDEBAR_PREF_WIDTH);
        sidebar.setPadding(new Insets(10));
        sidebar.setStyle("-fx-background-color: #444444;");
        sidebar.setSpacing(10);

        Button btn1 = new Button("按钮1");
        Button btn2 = new Button("按钮2");
        Button btn3 = new Button("按钮3");

        sidebar.getChildren().addAll(btn1, btn2, btn3);

        // 创建侧边栏动画效果
        TranslateTransition sidebarOpen = new TranslateTransition(Duration.millis(200), sidebar);
        sidebarOpen.setToX(0);

        TranslateTransition sidebarClose = new TranslateTransition(Duration.millis(200), sidebar);
        sidebarClose.setToX(-SIDEBAR_PREF_WIDTH);

        // 绑定按钮事件
        toggleSidebarBtn.setOnAction(event -> {
            if (sidebar.getTranslateX() != 0) {
                // 侧边栏处于关闭状态，需要打开
                sidebarOpen.play();
                toggleSidebarBtn.setText("关闭侧边栏");
            } else {
                // 侧边栏处于打开状态，需要关闭
                sidebarClose.play();
                toggleSidebarBtn.setText("打开侧边栏");
            }
        });

        root.setTop(topBox);
        root.setLeft(sidebar);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
